<template>
  <!-- <div class="main"> -->
  <!-- <mine-title>新建设备型号</mine-title> -->
  <!-- <div class="content" v-loading="loading"> -->
  <el-row :gutter="20">
    <el-col :span="18">
      <div>
        <el-form
          ref="form"
          :model="form"
          label-width="110px"
          :rules="rules"
          style="margin-left: 130px"
        >
          <el-form-item label="设备名称：" prop="name">
            <el-input v-model="form.name" size="small" clearable></el-input>
          </el-form-item>
          <el-form-item label="设备编号：" prop="typeCode">
            <el-input v-model="form.typeCode" size="small" clearable></el-input>
          </el-form-item>
          <el-form-item label="MTBF：" prop="mtbf">
            <el-input-number
              v-model="form.mtbf"
              size="small"
              clearable
              :precision="0"
            ></el-input-number>
          </el-form-item>
          <!-- <el-form-item label="是否LRU：" prop="productUnit">
                                <el-input
                                    v-model="form.productUnit"
                                    size="small"
                                    clearable
                                ></el-input>
                            </el-form-item> -->
          <el-form-item label="备注：" prop="remark">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>
          <el-form-item class="form-btn-row">
            <mine-button type="primary" @click="onCancel" secondary
              >取 消</mine-button
            >
            <mine-button type="primary" @click="onSubmit">保 存</mine-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
    <el-col :span="12">
      <!-- <el-table
                        class="table"
                        style="width: 100%"
                        ref="multipleTable"
                    >
                        <el-table-column
                            type="selection"
                            width="45"
                        ></el-table-column>
                        <el-table-column
                            label="序号"
                            show-overflow-tooltip
                            width="60"
                        >
                            <template slot-scope="scope">
                                <span
                                    >{{
                                        scope.$index +
                                            (currentPage - 1) * pageSize +
                                            1
                                    }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="文件"
                            prop="file"
                            show-overflow-tooltip
                            align="center"
                        >
                            <template slot="header">
                                <span>文件</span>
                            </template>
                        </el-table-column>
                    </el-table> -->
    </el-col>
  </el-row>
  <!-- </div> -->
  <!-- </div> -->
</template>

<script>
import request from "@/modules/index/utils/request";
import { QWebChannel } from "@/modules/index/utils/qwebchannel.js";
export default {
  name: "AddInstance",
  data() {
    return {
      form: {
        name: "",
        typeCode: "",
        mtbf: "",
        remark: "",
        creater: "",
        createTime: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请填写设备名称",
            trigger: "blur",
          },
        ],
        typeCode: [
          {
            required: true,
            message: "请填写设备名称",
            trigger: "blur",
          },
        ],
      },
      fileList: [],
      loading: false,
      downloading: false,
    };
  },
  computed: {
    accept() {
      return ".jpg, .jpeg, .png";
    },
  },
  methods: {
    onSubmit() {
      let _this = this;
      this.$refs.form.validate(async (valid, object) => {
        let { form } = _this;
        if (valid) {
          // console.log(form);
          this.loading = true;
          let res = {};
          let _self = this;
          if (_self.$qwebUtil.isQtClient) {
            const uuid = _self.$qwebUtil.generateRandomUUID();
            let sendObject;
            /* eslint-disable-next-line */
            const channel = new QWebChannel(
              window.qt.webChannelTransport,
              async function (channel) {
                sendObject = channel.objects.sendObject;
                // receivedJSON 修改此处发送请求参数
                const receivedJSON = {
                  RequestUrl: "/equipCatalog/save",
                  RequestMethod: "post",
                  ContentType: "application/json;charset=UTF-8",
                  RequestBody: form,
                  isStringParam: false,
                  requestId: uuid,
                };
                await send();
                await _self.$qwebUtil.receive(sendObject, receivedJSON);
                // console.error('received','/feCanvas/pageList')
              }
            );
            /* eslint-disable-next-line */
            async function send() {
              sendObject.sendMessage.connect(function (data) {
                if (!data) {
                  console.error("No data received.");
                }
                res = JSON.parse(data);
                const result = res.response || {};
                if (result.success && res.webInfo.requestId == uuid) {
                  _self.$message.success(result.msg);
                } else {
                  _self.$message.error(result.msg || "新建失败");
                  _self.loading = false;
                }
                _self.onCancel();
              });
            }
          } else if (this.$webview2.isWPF) {
            const uuid = this.$webview2.generateRandomUUID();
            await this.$webview2.sendWebView2(
              "/equipCatalog/save",
              "post",
              "application/json;charset=UTF-8",
              form,
              false,
              uuid
            );
            await window.chrome.webview.addEventListener(
              "message",
              (e) => {
                let res = e.data;
                let result = res.response;
                // console.log(result);
                this.onCancel();
                if (!result.success && res.response.uuid !== uuid) {
                  this.loading = false;
                  this.$message.error(result.msg || "新建失败");
                  return;
                }
                this.$message.success(result.msg);
              },
              {
                once: true,
              }
            );
          } else {
            request({
              url: "equipCatalog/save", //接口
              method: "post",
              data: form,
            }).then((response) => {
              let result = response.data;
              // console.log(response)
              this.onCancel();
              if (!result.success) {
                this.loading = false;
                this.$message.error(result.msg || "新建失败");
                return;
              }
              this.$message.success(result.msg);
            });
          }
        } else {
          return false;
        }
      });
    },
    onCancel() {
      this.form = {};
      this.$emit("changeFlag");
    },
  },
  // created() {
  //   this.$store.commit("SET_BREADCRUMB", [
  //     { title: "设备实例管理", path: "/equipAdmin/instance" },
  //     { title: "新建型号", path: "" },
  //   ]);
  // },
};
</script>

<style scoped></style>
